import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    inputTitleVal: "",
    inputMsgVal: "",
    fileList: [],
    travelId: "",
  },

  onInput(e) {
    this.setData({
      inputTitleVal: e.detail.value
    })
  },
  onMsgInput(e) {
    this.setData({
      inputMsgVal: e.detail.value
    })
  },

  //提交上传的图片
  submitImage(e) {
    const {
      rentImgIdList = []
    } = this.data;
    const {
      fileList = []
    } = this.data;
    let that = this;
    //提交之前先获取TravelId
    wx.request({
      url: 'http://localhost:8089/travel/getTravelId',
      success(res) {
        // console.log("拿到travelID:", res.data)
        wx.setStorageSync('travelId', res.data)
      }
    })
    //先提交表单内容
    // console.log("检查元素：", that.data.inputTitleVal, that.data.inputMsgVal)
    // console.log(fileList)
    // console.log(wx.getStorageSync('travelId'))
    // console.log("是否存在token:", wx.getStorageSync('token'))
    // 拿到TravelID
    if (wx.getStorageSync('travelId') != "" || wx.getStorageSync('travelId') != undefined) {
      Toast.loading({
        message: '发布中...',
        forbidClick: true,
      });
      //提交文件
      fileList.forEach(function (e) {
        // console.log("文件临时地址:", e.url)
        wx.uploadFile({
          //上传文件后台地址
          url: "http://localhost:8089/travel/uploadFile",
          method: 'POST',
          filePath: e.url,
          name: 'files',
          //表单数据
          formData: {
            token: wx.getStorageSync('token'),
            travelId: wx.getStorageSync('travelId'),
            travelTitle: that.data.inputTitleVal,
            travelMsg: that.data.inputMsgVal
          },
          success(res) {
            Toast.success('发布成功');
            wx.nextTick(callback => {
              wx.navigateBack();
            })
            // console.log("文件已上传！")
            // console.log(JSON.parse(res.data))
            var result = JSON.parse(res.data)
          },
          fail(res) {
            Toast.fail('发布失败');
            // console.log("上传fail");
          },
          complete(res) {
            // console.log("上传complete");
          }

        });

      });
    }
    that.setData({
      rentImgIdList: rentImgIdList
    });
    //置空travelID
    wx.removeStorageSync('travelId')
    console.log("submit image");
    console.log("submit image finish");
    //返回游记页面
  },
  deleteImage(event) {
    console.log(event);
    let that = this;
    const {
      fileList = []
    } = that.data;
    fileList.splice(event.detail.index, 1)
    that.setData({
      fileList
    });
  },
  afterRead(event) {
    console.log(event.detail);
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    // 上传完成需要更新 fileList
    const {
      fileList = []
    } = this.data;
    console.log(file);
    file.forEach(function (e) {
      fileList.push({
        ...file,
        url: e.url
      });
    });

    this.setData({
      fileList
    });
    console.log(this.data.fileList);
  },
});